<template>
  <div class="pad-20">
    <div>
      <div class="pad-b-20">
        <el-button type="primary" @click="saveBrandCategory()">新增分类</el-button>
      </div>
      <div class="top-operate-default">
        <div class="search-item">
          <label>搜索分类：</label>
          <el-input @keyup.enter.native="handleSearch" v-model="keyworkedName" placeholder="请输入分类名称" style="width: 200px;"></el-input>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-button-group>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container mar-t-20">
        <el-table ref="multipleTable" :data="entityList" v-loading="listLoading" element-loading-text="加载中" style="width: 100%">
          <el-table-column label="分类名称">
            <template slot-scope="scope">
              <div class="cursor-p" style="color:#155bd4;" @click="saveBrandCategory(scope.row)">{{scope.row.brandCategoryName}}</div>
            </template>
          </el-table-column>
          <el-table-column label="描述" prop="brandDescribe"></el-table-column>
          <el-table-column label="品牌数" prop="brandNum"></el-table-column>
          <el-table-column width="140px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="saveBrandCategory(scope.row)">编辑</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="delBrandCategory(scope.row)"
              >
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange"
           @current-change="handleCurrentChange" :current-page="entityListQuery.currentPage"
           :page-sizes="[10, 20, 50, 100]" :page-size="entityListQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <!--新增-->
    <el-dialog title="" :visible.sync="entityVisible" width="650px">
      <el-form ref="form" :model="entity" :rules="rules" label-position="right" label-width="100px" style='width: 500px; margin-left:50px;'>
        <el-form-item label="分类名称" prop="brandCategoryName">
          <el-input v-model="entity.brandCategoryName" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="brandDescribe">
          <el-input type="textarea" :rows="6" placeholder="请输入" v-model="entity.brandDescribe"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveEntity">确认</el-button>
        <el-button @click="entityVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { pageBrandCategory, saveBrandCategory, delBrandCategory } from '@/api/user/enterpriseBrand'
  export default {
    data() {
      return {
        brandType: '0',
        entityList: [],
        total: null,
        keyworkedName: '',
        entityListQuery: {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        },
        listCategory: [],
        listLoading: false,
        entity: {
          brandCategoryId: '',
          enterpriseId: this.getEnterpriseId(),
          brandCategoryName: '',
          brandDescribe: '',
          brandNum: ''
        },
        entityVisible: false,
        rules: {
          brandCategoryName: [
            { required: true, message: '请填写分类名称', trigger: 'blur' },
            { max: 32, message: '分类名称最多输入32个字符' }
          ]
        }
      }
    },
    created() {
      this.initPage()
    },
    methods: {
      handleClick() {
        this.entityListQuery = {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        }
        if (this.brandType === '1') {
          this.listBrandCategory()
        }
        this.initPage()
      },
      handleSearch() {
        this.initPage()
      },
      handleReset() {
        this.entityListQuery = {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        }
        this.keyworkedName = ''
        this.initPage()
      },
      delBrandCategory(row) {
        delBrandCategory(row.brandCategoryId).then(response => {
          if (response.isError) return
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.initPage()
        })
      },
      saveBrandCategory(row) {
        this.entity = {}
        if (row) {
          this.entity = JSON.parse(JSON.stringify(row))
        }
        this.entityVisible = true
      },
      saveEntity() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.entity.enterpriseId = this.getEnterpriseId()
            saveBrandCategory(this.entity).then(response => {
              if (response.isError) return
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.initPage()
              this.entityVisible = false
            })
          }
        })
      },
      initPage() {
        this.listLoading = true
        this.entityListQuery['brandCategoryName'] = this.keyworkedName
        pageBrandCategory(this.entityListQuery).then(response => {
          if (response.isError) return
          this.entityList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.entityListQuery.currentPage = val
        this.initPage()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.entityListQuery.size = val
        this.initPage()
      },
      // 勾选
      handleSelectionChange(val) {
        this.multipleSelection = val
      }
    }
  }
</script>

<style lang='less' scoped>
  .user-avatar {
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }
  .align-space{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-left: 10px;
    line-height: 1.2;
  }
</style>
